package com.gdcp.service.impl;

import com.gdcp.beans.Product;
import com.gdcp.beans.Shop;
import com.gdcp.beans.User;
import com.gdcp.common.Const;
import com.gdcp.common.ResponseModel;
import com.gdcp.common.page.PageBean;
import com.gdcp.common.page.PageConstants;
import com.gdcp.config.RequestHolder;
import com.gdcp.config.annotation.RedisCache;
import com.gdcp.mapper.ShopMapper;
import com.gdcp.mapper.UserMapper;
import com.gdcp.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ShopServiceImpl implements ShopService {

    @Autowired
    ShopMapper shopMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    ResponseModel responseModel;

    @Override
    public Shop queryShopInfoByShopId(Integer shop_id) {
        List<Shop> shops=shopMapper.queryShopInfoByShopId(shop_id,null);
        for (Shop shop:shops){
            return shop;
        }
        return null;
    }

    @Override
    public Map<String,Object> insert(Shop shop) {
        try{
            shopMapper.insert(shop);
        }catch (Exception e){
            e.printStackTrace();
            return responseModel.setModel(false,"插入失败"+e.toString());
        }

        return responseModel.setModel(true,"插入成功");
    }

    @Override
    public Map<String,Object> updateByShopId(Shop shop) {
        try{
            shopMapper.updateByShopId(shop);
        }catch (Exception e){
            e.printStackTrace();
            return responseModel.setModel(false,"保存失败"+e.toString());
        }
        return responseModel.setModel(true,"保存成功");
    }

    @Override
    @Transactional(rollbackFor = { Exception.class })
    public int registerShop(Shop shop) {
        try{
            shopMapper.registerShop(shop);
            /*User user=new User();
            user.setShop_id(shop.getId());
            userMapper.updateByPrimaryKey(user);*/
        }catch (Exception e){
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return 0;
        }
        return 1;
    }

    @Override
    public Shop queryShopInfoByUserId(Integer user_id) {
        List<Shop> shops=shopMapper.queryShopInfoByShopId(null,user_id);
        for (Shop shop:shops){
            return shop;
        }
        return null;
    }

    @Override
    @RedisCache(flush = true)
    public List<Shop> queryByNewTime() {
        return shopMapper.queryByNewTime();
    }

    @Override
    public int updateSaleCount(Integer id) {
        try{
            shopMapper.updateSaleCount(id);
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
        return 1;
    }

    @Override
    public int updatePopularity(Integer id) {
        try{
            shopMapper.updatePopularity(id);
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
        return 1;
    }

    @Override
    public Map<String, Object> findPageBreakByCondition(Map<String, Object> model) {
        Map<String,Object> result = new HashMap<>();
        int total=shopMapper.findPageBreakByCondition(null).size();
        List<Shop> rows=shopMapper.findPageBreakByCondition(model);
        if (rows.size()==0){
            int count= (int) model.get("a");
            count=count-(int) model.get("b");
            if (count>0){
                model.put("a",count);
                rows=shopMapper.findPageBreakByCondition(model);
            }
        }
        result.put("total",total);
        result.put("rows",rows);
        return result;
    }

    @Override
    @Transactional(rollbackFor = { Exception.class })
    public Map<String,Object> removeByPrimaryKey(Integer[] ids) {
        try{
            for (Integer id:ids){
                shopMapper.removeByPrimaryKey(id);
            }
        }catch (Exception e){
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return responseModel.setModel(false, "删除["+ids.length+"条记录失败"+e.toString());
        }
        return responseModel.setModel(true, "成功删除["+ids.length+"条记录");
    }

    @Override
    @Transactional(rollbackFor = { Exception.class })
    public int register(Shop shop) {
        try{
            User user1= (User) RequestHolder.getSession("user");
            shopMapper.insert(shop);
            User user=new User();
            user.setUsername(user1.getUsername());
            user.setShop_id(shop.getId());
            user.setPhone(user1.getPhone());
            user.setEmail(user1.getEmail());
            userMapper.updateByPrimaryKey(user);
        }catch (Exception e){
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return 0;
        }
        return 1;
    }

    @Override
    public Map<String,Object> updateInformation(Shop shop) {
        try{
            shopMapper.updateByShopId(shop);
        }catch (Exception e){
            e.printStackTrace();
            return responseModel.setModel(false, "店铺修改失败，"+e.toString());
        }
        return responseModel.setModel(true, "修改成功");
    }

    @Override
    //@RedisCache(flush = true)
    public Shop getInformation(Integer id) {
        return shopMapper.queryInfoById(id);
    }

    @Override
    public Map<String, Object> searchInformation(Map<String, Object> model) {
        Map<String,Object> result = new HashMap<>();
        int total=shopMapper.searchInformation(null).size();
        List<Shop> rows=shopMapper.searchInformation(model);
        result.put("total",total);
        result.put("rows",rows);
        return result;
    }

    @Override
    public PageBean<Shop> searchByShop(Integer cid, int pc, String search_key, String column, String sort) {
        PageBean<Shop> pb = new PageBean<>();
        pb.setPc(pc);

        int ps = PageConstants.PRODUCT_PAGE_SIZE;
        pb.setPs(ps);

        int tr=shopMapper.getPageNumber(cid).size();
        pb.setTr(tr);

        pb.setTp(pb.getTp());

        List<Shop> shops=shopMapper.searchByShop(cid,(pc-1)*ps,ps,search_key,column,sort);
        pb.setBeanList(shops);
        return pb;
    }

    @Override
    public List<Shop> queryByAll() {
        return shopMapper.queryByAll();
    }
}
